<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="14" :offset="-1">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
        <el-col :span="3" class="bt_area">
          <div class="bt_item">
            <el-tooltip effect="dark" content="筛选" placement="top-start">
              <el-button
                plain
                icon="el-icon-s-operation"
                size="small"
              ></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="导出" placement="top-start">
              <el-button
                plain
                icon="el-icon-data-analysis"
                size="small"
              ></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="打印" placement="top-start">
              <el-button plain icon="el-icon-printer" size="small"></el-button>
            </el-tooltip>
          </div>
        </el-col>
      </el-row>

      <!-- table表格区域 -->
      <el-table
        :data="goodList"
        border
        stripe
        @expand-change="expandChange"
        ref="table"
        :expand-row-keys="expands"
        :row-key="getRowKeys"
      >
        <el-table-column type="expand" label="展开" width="1">
          <template>
            <!-- <span>{{ goodInfo }}</span> -->
            <el-descriptions
              class="margin-top descr"
              title="详细信息"
              :column="4"
              border
            >
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-monitor"></i>
                  商品名
                </template>
                {{ goodInfo.goods_name }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-data-analysis"></i>
                  价格
                </template>
                {{ goodInfo.goods_price }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-s-data"></i>
                  数量
                </template>
                {{ goodInfo.goods_number }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label">
                  <i class="el-icon-odometer"></i>
                  重量
                </template>
                {{ goodInfo.goods_weight }}
              </el-descriptions-item>
              <el-descriptions-item
                labelClassName="my-label"
                contentClassName="my-content"
              >
                <template slot="label">
                  <i class="el-icon-tickets"></i>
                  参数
                </template>
                <el-descriptions border title="动态参数">
                  <el-descriptions-item
                    v-for="(item, i) in attribute"
                    :key="i"
                    :label="item.attr_name"
                    >{{ item.attr_vals }}</el-descriptions-item
                  >
                </el-descriptions>
                <el-descriptions border title="静态属性">
                  <el-descriptions-item
                    v-for="(itemp, i) in parameter"
                    :key="i"
                    :label="itemp.attr_name"
                    >{{ itemp.attr_vals }}</el-descriptions-item
                  >
                </el-descriptions>
              </el-descriptions-item>
              <el-descriptions-item contentClassName="introduce-content">
                <template slot="label">
                  <i class="el-icon-postcard"></i>
                  简介
                </template>
                {{ goodInfo.goods_introduce }}
              </el-descriptions-item>
              <el-descriptions-item>
                <template slot="label" contentClassName="img-content">
                  <i class="el-icon-picture-outline"></i>
                  缩略图
                </template>
                <img
                  v-for="(itemimg, i) in goodInfo.pics"
                  :key="i"
                  :src="itemimg.pics_mid_url"
                  alt=""
                  width="150px"
                  height="150px"
                  class="imgcard"
                />
              </el-descriptions-item>
            </el-descriptions>
          </template>
        </el-table-column>
        <el-table-column type="index" label="索引"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格(元)"
          width="110px"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="goods_weight"
          label="商品重量"
          width="110px"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="add_time"
          label="创建时间"
          width="250px"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              plain
              icon="el-icon-s-data"
              @click.native="show_good(scope.row)"
              >查看</el-button
            >
            <el-button
              type="danger"
              size="mini"
              plain
              icon="el-icon-delete"
              @click="removeById(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 商品详情 -->
    <!-- <el-dialog title="提示" :visible.sync="dialogVisible" width="50%">
      <span>商品详情</span>
      <div v-if="dialogVisible">
        <img
          v-if="goodInfo.pics.length !== 0"
          :src="goodInfo.pics[0].pics_mid_url"
          alt=""
        />
        <img
          v-else-if="goodInfo.pics.length == 0"
          src="@/assets/we.jpg"
          width="60%"
          height="60%"
          alt=""
        />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" plain size="small"
          >取 消</el-button
        >
        <el-button
          type="primary"
          @click="dialogVisible = false"
          plain
          size="small"
          >确 定</el-button
        >
      </span>
    </el-dialog> -->
  </div>
</template>

<script>
// import { mapState } from 'vuex'
export default {
  name: 'GoodsList',
  data() {
    return {
      //查询参数对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      //商品列表
      goodList: [],
      goodInfo: [],
      parameter: [], //静态参数
      attribute: [], //动态属性
      //总数据条数
      total: 0,
      expands: [], //展开行的keys数组
      isHavaValue: false, //静态属性是否有值
    }
  },
  // computed: {
  //   ...mapState(['goodInfo']),
  // },
  created() {
    this.getGoodsList()
  },
  // filters:{
  //   showThisInfo:function(value,rowID){
  //     console.log('value',value[0].goods_id)
  //     console.log('rowID',rowID)
  //     return value
  //   }
  // },
  methods: {
    //根据分页获取对应的商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败！')
      }
      this.$message.success('获取商品列表成功！')
      this.goodList = res.data.goods
      this.total = res.data.total
    },
    //
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    //删除按钮
    async removeById(id) {
      //弹框提示用户是否要删除
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
      ).catch((error) => {
        return error
      })
      //如果用户确认删除返回值为字符串 confirm,用户取消则返回字符串 cancel
      if (confirmResult !== 'confirm') {
        return this.$message.info('已经取消删除')
      }
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败！')
      }
      this.getGoodsList()
      this.$message.success('删除成功！')
    },
    //添加商品
    goAddpage() {
      this.$router.push('/goods/add')
    },
    //查看商品详情
    // async show_good(row, expandedRows) {
    //   console.log(expandedRows.length)
    //   // this.$store.dispatch('getGoodInfo', row.goods_id)
    //   if (expandedRows.length > 1) {
    //     const res = await this.$http.get(`goods/${row.goods_id}`)
    //     console.log('res', res.data.data)
    //     this.goodInfo = res.data.data
    //     //如果当前的页面ID和其他打开ID不一样则关闭其他的
    //     expandedRows.forEach((item) => {
    //       if (row.goods_id != item.goods_id) {
    //         this.$refs.table.toggleRowExpansion(item, false)
    //       }
    //     })
    //   } else {
    //     const res = await this.$http.get(`goods/${row.goods_id}`)
    //     console.log('res', res.data.data)
    //     this.goodInfo = res.data.data
    //   }
    //   // this.$store.dispatch('getGoodInfo', row.goods_id)
    // },
    show_good(row) {
      this.$refs.table.toggleRowExpansion(row)
      this.parameter = []
      this.attribute = []
    },
    async expandChange(row, expandedRows) {
      const res = await this.$http.get(`goods/${row.goods_id}`)
      console.log(res.data.data)
      res.data.data.attrs.forEach((item) => {
        if (item.attr_sel == 'many') {
          this.parameter.push(item)
        } else {
          this.attribute.push(item)
        }
      })
      if (this.parameter) {
        this.isHavaValue = true
      } else return
      // console.log('isHavaValue',this.isHavaValue)
      // console.log('parameter', this.parameter)
      // console.log('attribute', this.attribute)
      // if (res.data.meta.status == 200) {
      //   this.$message.success('获取详情成功')
      // } else {
      //   this.$message.error('获取详情失败')
      // }
      let that = this
      if (expandedRows.length) {
        //此时展开
        that.expands = []
        that.goodInfo = []
        if (row) {
          that.expands.push(row.goods_id)
          that.goodInfo = res.data.data
        }
      } else {
        //折叠
        that.expands = []
        that.goodInfo = []
      }
    },
    getRowKeys(row) {
      //行数据的key，用于优化table的渲染
      return row.goods_id
    },
  },
}
</script>

<style>
.bt_area {
  height: 40px;
  border: 1px solid #eee;
  padding-top: 3px;
}
.bt_item {
  padding-left: 25px;
}
.descr {
  width: 95%;
  margin-left: 2.5%;
  max-height: 600px !important;
  overflow: auto;
}
.my-label {
  width: 15%;
}
.my-content {
  width: 50%;
}
.introduce-content {
  overflow: auto;
  text-overflow: ellipsis;
}
.img-content {
  width: 100px;
  height: 100px;
}
.imgcard {
  margin: 15px auto;
  border: 1px solid #eee;
}
</style>